package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.InvitationJoin;
import com.tbit.uqbike.object.pojo.statistics.ExportInvitationJoin;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.InvitationJoinService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelWrapper;
import com.tbit.utils.DateTimeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.RequestContext;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 活动参与(InvitationJoin)表控制层
 *
 * @author jing
 * @since 2020-12-31 14:34:17
 */
@RestController
@RequestMapping("/invitationjoin")
public class InvitationJoinController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private InvitationJoinService invitationJoinService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/selectList")
    public JsonResult selectList(Integer id) {
        return JsonResult.ok(this.invitationJoinService.queryById(id));
    }

    @PostMapping("/selectByAccountId")
    public JsonResult selectByAccountId(InvitationJoin invitationJoin) {
        Assert.notNull(invitationJoin.getAccountId(), "ver.areaId.notNull");
        invitationJoin.setPage(new Page(invitationJoin.getPageNO(), invitationJoin.getRowCount()));
        PageHelperUtil.initPage(invitationJoin.getPage());
        PageInfo<InvitationJoin> pageInfo = new PageInfo(invitationJoinService.queryByInvitationJoin(invitationJoin));
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @GetMapping("/exportByAccountId")
    public void exportByAccountId(InvitationJoin invitationJoin, HttpServletResponse response) {
        Assert.notNull(invitationJoin.getAccountId(), "ver.areaId.notNull");
        List<ExportInvitationJoin> exports = invitationJoinService.exportByAccountId(invitationJoin);
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        ExportExcelWrapper<ExportInvitationJoin> util = new ExportExcelWrapper<>();
        String filename = context.getMessage("activity_join") + DateTimeUtil.getNowTimeYMD();
        String[] columnNames = {context.getMessage("activity_name"), context.getMessage("invitation_phone"), context.getMessage("accepted_phone"), context.getMessage("accepted_time"), context.getMessage("cutoff_time")};
        util.exportExcel(filename, filename, columnNames, exports, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

}